home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / base / service.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.7 KB  |  238 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''GBaseService extends the GDataService to streamline Google Base operations.
  5.  
  6.   GBaseService: Provides methods to query feeds and manipulate items. Extends 
  7.                 GDataService.
  8.  
  9.   DictionaryToParamList: Function which converts a dictionary into a list of 
  10.                          URL arguments (represented as strings). This is a 
  11.                          utility function used in CRUD operations.
  12. '''
  13. __author__ = 'api.jscudder (Jeffrey Scudder)'
  14. import urllib
  15. import gdata
  16. import atom.service as atom
  17. import gdata.service as gdata
  18. import gdata.base as gdata
  19. import atom
  20. BASE_BATCH_URL = 'http://www.google.com/base/feeds/items/batch'
  21.  
  22. class Error(Exception):
  23.     pass
  24.  
  25.  
  26. class RequestError(Error):
  27.     pass
  28.  
  29.  
  30. class GBaseService(gdata.service.GDataService):
  31.     '''Client for the Google Base service.'''
  32.     
  33.     def __init__(self, email = None, password = None, source = None, server = 'base.google.com', api_key = None, additional_headers = None, handler = None, **kwargs):
  34.         """Creates a client for the Google Base service.
  35.  
  36.     Args:
  37.       email: string (optional) The user's email address, used for
  38.           authentication.
  39.       password: string (optional) The user's password.
  40.       source: string (optional) The name of the user's application.
  41.       server: string (optional) The name of the server to which a connection
  42.           will be opened. Default value: 'base.google.com'.
  43.       api_key: string (optional) The Google Base API key to use.
  44.       **kwargs: The other parameters to pass to gdata.service.GDataService
  45.           constructor.
  46.     """
  47.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'gbase', source = source, server = server, additional_headers = additional_headers, handler = handler, **kwargs)
  48.         self.api_key = api_key
  49.  
  50.     
  51.     def _SetAPIKey(self, api_key):
  52.         if not isinstance(self.additional_headers, dict):
  53.             self.additional_headers = { }
  54.         
  55.         self.additional_headers['X-Google-Key'] = api_key
  56.  
  57.     
  58.     def __SetAPIKey(self, api_key):
  59.         self._SetAPIKey(api_key)
  60.  
  61.     
  62.     def _GetAPIKey(self):
  63.         if 'X-Google-Key' not in self.additional_headers:
  64.             return None
  65.         return self.additional_headers['X-Google-Key']
  66.  
  67.     
  68.     def __GetAPIKey(self):
  69.         return self._GetAPIKey()
  70.  
  71.     api_key = property(__GetAPIKey, __SetAPIKey, doc = 'Get or set the API key to be included in all requests.')
  72.     
  73.     def Query(self, uri, converter = None):
  74.         """Performs a style query and returns a resulting feed or entry.
  75.  
  76.     Args:
  77.       uri: string The full URI which be queried. Examples include
  78.           '/base/feeds/snippets?bq=digital+camera', 
  79.           'http://www.google.com/base/feeds/snippets?bq=digital+camera'
  80.           '/base/feeds/items'
  81.           I recommend creating a URI using a query class.
  82.       converter: func (optional) A function which will be executed on the
  83.           server's response. Examples include GBaseItemFromString, etc. 
  84.  
  85.     Returns:
  86.       If converter was specified, returns the results of calling converter on
  87.       the server's response. If converter was not specified, and the result
  88.       was an Atom Entry, returns a GBaseItem, by default, the method returns
  89.       the result of calling gdata.service's Get method.
  90.     """
  91.         result = self.Get(uri, converter = converter)
  92.         if converter:
  93.             return result
  94.         if isinstance(result, atom.Entry):
  95.             return gdata.base.GBaseItemFromString(result.ToString())
  96.         return result
  97.  
  98.     
  99.     def QuerySnippetsFeed(self, uri):
  100.         return self.Get(uri, converter = gdata.base.GBaseSnippetFeedFromString)
  101.  
  102.     
  103.     def QueryItemsFeed(self, uri):
  104.         return self.Get(uri, converter = gdata.base.GBaseItemFeedFromString)
  105.  
  106.     
  107.     def QueryAttributesFeed(self, uri):
  108.         return self.Get(uri, converter = gdata.base.GBaseAttributesFeedFromString)
  109.  
  110.     
  111.     def QueryItemTypesFeed(self, uri):
  112.         return self.Get(uri, converter = gdata.base.GBaseItemTypesFeedFromString)
  113.  
  114.     
  115.     def QueryLocalesFeed(self, uri):
  116.         return self.Get(uri, converter = gdata.base.GBaseLocalesFeedFromString)
  117.  
  118.     
  119.     def GetItem(self, uri):
  120.         return self.Get(uri, converter = gdata.base.GBaseItemFromString)
  121.  
  122.     
  123.     def GetSnippet(self, uri):
  124.         return self.Get(uri, converter = gdata.base.GBaseSnippetFromString)
  125.  
  126.     
  127.     def GetAttribute(self, uri):
  128.         return self.Get(uri, converter = gdata.base.GBaseAttributeEntryFromString)
  129.  
  130.     
  131.     def GetItemType(self, uri):
  132.         return self.Get(uri, converter = gdata.base.GBaseItemTypeEntryFromString)
  133.  
  134.     
  135.     def GetLocale(self, uri):
  136.         return self.Get(uri, converter = gdata.base.GDataEntryFromString)
  137.  
  138.     
  139.     def InsertItem(self, new_item, url_params = None, escape_params = True, converter = None):
  140.         """Adds an item to Google Base.
  141.  
  142.     Args: 
  143.       new_item: atom.Entry or subclass A new item which is to be added to 
  144.                 Google Base.
  145.       url_params: dict (optional) Additional URL parameters to be included
  146.                   in the insertion request. 
  147.       escape_params: boolean (optional) If true, the url_parameters will be
  148.                      escaped before they are included in the request.
  149.       converter: func (optional) Function which is executed on the server's
  150.           response before it is returned. Usually this is a function like
  151.           GBaseItemFromString which will parse the response and turn it into
  152.           an object.
  153.  
  154.     Returns:
  155.       If converter is defined, the results of running converter on the server's
  156.       response. Otherwise, it will be a GBaseItem.
  157.     """
  158.         response = self.Post(new_item, '/base/feeds/items', url_params = url_params, escape_params = escape_params, converter = converter)
  159.         if not converter and isinstance(response, atom.Entry):
  160.             return gdata.base.GBaseItemFromString(response.ToString())
  161.         return response
  162.  
  163.     
  164.     def DeleteItem(self, item_id, url_params = None, escape_params = True):
  165.         """Removes an item with the specified ID from Google Base.
  166.  
  167.     Args:
  168.       item_id: string The ID of the item to be deleted. Example:
  169.                'http://www.google.com/base/feeds/items/13185446517496042648'
  170.       url_params: dict (optional) Additional URL parameters to be included
  171.                   in the deletion request.
  172.       escape_params: boolean (optional) If true, the url_parameters will be
  173.                      escaped before they are included in the request.
  174.  
  175.     Returns:
  176.       True if the delete succeeded.
  177.     """
  178.         return self.Delete('%s' % (item_id[len('http://www.google.com'):],), url_params = url_params, escape_params = escape_params)
  179.  
  180.     
  181.     def UpdateItem(self, item_id, updated_item, url_params = None, escape_params = True, converter = gdata.base.GBaseItemFromString):
  182.         """Updates an existing item.
  183.  
  184.     Args:
  185.       item_id: string The ID of the item to be updated.  Example:
  186.                'http://www.google.com/base/feeds/items/13185446517496042648'
  187.       updated_item: atom.Entry, subclass, or string, containing
  188.                     the Atom Entry which will replace the base item which is 
  189.                     stored at the item_id.
  190.       url_params: dict (optional) Additional URL parameters to be included
  191.                   in the update request.
  192.       escape_params: boolean (optional) If true, the url_parameters will be
  193.                      escaped before they are included in the request.
  194.       converter: func (optional) Function which is executed on the server's
  195.           response before it is returned. Usually this is a function like
  196.           GBaseItemFromString which will parse the response and turn it into
  197.           an object.
  198.  
  199.     Returns:
  200.       If converter is defined, the results of running converter on the server's
  201.       response. Otherwise, it will be a GBaseItem.
  202.     """
  203.         response = self.Put(updated_item, item_id, url_params = url_params, escape_params = escape_params, converter = converter)
  204.         if not converter and isinstance(response, atom.Entry):
  205.             return gdata.base.GBaseItemFromString(response.ToString())
  206.         return response
  207.  
  208.     
  209.     def ExecuteBatch(self, batch_feed, converter = gdata.base.GBaseItemFeedFromString):
  210.         """Sends a batch request feed to the server.
  211.     
  212.     Args: 
  213.       batch_feed: gdata.BatchFeed A feed containing BatchEntry elements which
  214.           contain the desired CRUD operation and any necessary entry data.
  215.       converter: Function (optional) Function to be executed on the server's
  216.           response. This function should take one string as a parameter. The
  217.           default value is GBaseItemFeedFromString which will turn the result 
  218.           into a gdata.base.GBaseItem object.
  219.  
  220.     Returns:
  221.       A gdata.BatchFeed containing the results.
  222.     """
  223.         return self.Post(batch_feed, BASE_BATCH_URL, converter = converter)
  224.  
  225.  
  226.  
  227. class BaseQuery(gdata.service.Query):
  228.     
  229.     def _GetBaseQuery(self):
  230.         return self['bq']
  231.  
  232.     
  233.     def _SetBaseQuery(self, base_query):
  234.         self['bq'] = base_query
  235.  
  236.     bq = property(_GetBaseQuery, _SetBaseQuery, doc = 'The bq query parameter')
  237.  
  238.